package com.sponge.utils;

import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class MathUtils {


    public long getFixedLengthNumber(int length){
        Random random = new Random();
        String[] first = {"1","2","3","4","5","6","7","8","9"};
        String[] other = {"0","1","2","3","4","5","6","7","8","9"};

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(first[random.nextInt(first.length)]);
        for (int i = 0; i < length-1; i++) {
            stringBuffer.append(other[random.nextInt(other.length)]);
        }
        System.out.println(stringBuffer);
        return  Long.parseLong(stringBuffer.toString());

    }

    public long getFixedLengthNumberByChar(int length){
        Random random = new Random();
        char[] firstNumber = {'1','2','3','4','5','6','7','8','9'};
        char[] otherNumber = {'0','1','2','3','4','5','6','7','8','9'};

        char[] randomId = new char[length];
        randomId[0] = firstNumber[random.nextInt(9)];
        for (int i = 0; i < length - 1; i++) {
            randomId[i+1] = otherNumber[random.nextInt(10)];
        }
//        System.out.println(new String(randomId));
        return  Long.parseLong(new String(randomId));
    }

    public String getFixedLengthNumber3(int length){
        Random rm = new Random();

        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, length);

        // 将获得的获得随机数转化为字符串
        String fixLenthString = String.valueOf(pross);

        // 返回固定的长度的随机数
        return fixLenthString.substring(1, length + 1);
    }


}
